# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------

library(data.table)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Just in case something masks 'source()', use extra deliberate base::source().
base::source("~/code/0-utility-functions/table-and-text-utils.R", local = TRUE)

# DECLARE DATA FROM OUR AND COMPARISON STUDIES ---------------------------------
# All values reported in Table 6.1 from comparison studies are directly reported
# in the relevant papers.

comp_dt <- list()

# IRS (2001)
# Respondents to a Survey in Massachusetts
# Do not report wealth effects in their paper.
comp_dt[[1]] <-
    data.table(
        study = "IRS (2001)",
        src_pop_1 = "Respondents to a Survey in Massachusetts",
        src_pop_2 = NA,
        posttax_hh_total_estimate = as.numeric(NA),
        posttax_hh_total_se = as.numeric(NA),
        posttax_winner_wage_estimate = as.numeric(NA),
        posttax_winner_wage_se = as.numeric(NA),
        pretax_hh_wage_estimate = as.numeric(NA),
        pretax_hh_wage_se = as.numeric(NA)
    )

# BFGI (2021)
# Administrative Data on Parents with Young to Early-Adult Children
# This estimate corresponds to the estimate in their Appendix
# Table A35, which is also mentioned in their footnote 41.
comp_dt[[2]] <-
    data.table(
        study = "BFGI (2021)",
        src_pop_1 = "Administrative Data on",
        src_pop_2 = "Parents with Young to Early-Adult Children",
        posttax_hh_total_estimate = as.numeric(NA),
        posttax_hh_total_se = as.numeric(NA),
        posttax_winner_wage_estimate = as.numeric(NA),
        posttax_winner_wage_se = as.numeric(NA),
        pretax_hh_wage_estimate = -1.171,
        pretax_hh_wage_se = 0.179
    )

# Our estimate
# The estimate in Column 3 in Table 6.1 corresponds to the estimate we reported
# earlier in Table 3.1. The other two estimates are also obtained directly
# on our data.
comp_dt[[3]] <-
    data.table(
        study = "Our estimate",
        src_pop_1 = "Administrative Data on",
        src_pop_2 = "Working-Age Individuals and Households",
        posttax_hh_total_estimate = -2.339,
        posttax_hh_total_se = 0.066,
        posttax_winner_wage_estimate = -1.717,
        posttax_winner_wage_se = 0.032,
        pretax_hh_wage_estimate = -1.288,
        pretax_hh_wage_se = 0.042
    )

# CLNÖ (2017)
# The estimates corresponds to their estimate in their Table 6, Column 5, and
# their Table 3, Column 2, respectively.
comp_dt[[4]] <-
    data.table(
        study = "CLNÖ (2017)",
        src_pop_1 = "Administrative Data on",
        src_pop_2 = "Working-Age Individuals and Households",
        posttax_hh_total_estimate = -1.306,
        posttax_hh_total_se = 0.194,
        posttax_winner_wage_estimate = -0.964,
        posttax_winner_wage_se = 0.151,
        pretax_hh_wage_estimate = as.numeric(NA),
        pretax_hh_wage_se = as.numeric(NA)
    )

# PSO (2018)
# The estimate corresponds to the average of the estimates reported in their
# Appendix D, Table D1, for the first three years after winning.
comp_dt[[5]] <-
    data.table(
        study = "PSO (2018)",
        src_pop_1 = "Administrative Data on",
        src_pop_2 = "Working-Age Individuals and Households",
        posttax_hh_total_estimate = as.numeric(NA),
        posttax_hh_total_se = as.numeric(NA),
        posttax_winner_wage_estimate = -1 * mean(c(0.070, 0.113, 0.118)) * 10,
        posttax_winner_wage_se = mean(c(0.069, 0.058, 0.062)) * 10,
        pretax_hh_wage_estimate = as.numeric(NA),
        pretax_hh_wage_se = as.numeric(NA)
    )

comp_dt <- rbindlist(comp_dt, use.names = TRUE)

# MAKE TABLE -------------------------------------------------------------------

filedir <- "~/paper/tables/table-6.1.tex"
file.create(filedir)

# Start Table 6.1
start_latex_table(
    column_structure = "lccccc",
    file_dir = filedir,
    append = TRUE
)
add_latex_table_rule(rule_type = "toprule", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

panel_a_heading_input1a <-
    " &  & \\multicolumn{2}{c}{Post-Tax Winnings} & & Pre-Tax Winnings" #nolint
write_latex_table_row(
    panel_a_heading_input1a,
    file_dir = filedir,
    append = TRUE
)
add_latex_cmidrule(
    point_start_col = "l",
    point_end_col = "r",
    start_col = 3,
    end_col = 4,
    file_dir = filedir,
    append = TRUE
)
add_latex_cmidrule(
    point_start_col = "l",
    point_end_col = "r",
    start_col = 6,
    end_col = 6,
    file_dir = filedir,
    append = TRUE
)

panel_a_heading_input1b <-
    "  & Primary Data Source and & Household & Winner & & Household" #nolint
write_latex_table_row(
    panel_a_heading_input1b,
    file_dir = filedir,
    append = TRUE
)

panel_a_heading_input1b <-
    "Study & Population of Study  & Total Labor Earnings & Wage Earnings & & Wage Earnings" #nolint
write_latex_table_row(
    panel_a_heading_input1b,
    file_dir = filedir,
    append = TRUE
)

panel_a_heading_input1c <-
    "(1) & (2) & (3) & (4) & & (5)" #nolint
write_latex_table_row(
    panel_a_heading_input1c,
    file_dir = filedir,
    append = TRUE
)

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

# Add body of Table 6.1
# - rows too distinct, no convenience functions here

write_latex_table_row(
    "\\emph{Panel A: U.S. Studies} &  &  &  &  &  ",
    file_dir = filedir,
    append = TRUE
)
write_latex_table_row(
        "  &  &  &  &  & ",
        file_dir = filedir,
        short_row_height = TRUE,
        append = TRUE
    )

panel_a_row1_input <-
    sprintf(
        "\\multirow{2}{*}{%s} & \\multirow{2}{*}{%s} & \\multirow{2}{*}{%s} & \\multirow{2}{*}{%s} & & \\multirow{2}{*}{%s}", #nolint
        comp_dt[study == "IRS (2001)"]$study,
        comp_dt[study == "IRS (2001)"]$src_pop_1,
        fmt_fixed_decimal(
            input = comp_dt[study == "IRS (2001)"]$posttax_hh_total_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "IRS (2001)"]$posttax_winner_wage_estimate, #nolint
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "IRS (2001)"]$pretax_hh_wage_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_a_row1_input, file_dir = filedir, append = TRUE)

write_latex_table_row(
        "  &  &  &  &  & ",
        file_dir = filedir,
        short_row_height = FALSE,
        append = TRUE
    )

panel_a_row2_input_a <-
    sprintf(
        "\\multirow{2}{*}{%s} & %s & \\multirow{2}{*}{%s} & \\multirow{2}{*}{%s} & & %s", #nolint
        comp_dt[study == "BFGI (2021)"]$study,
        comp_dt[study == "BFGI (2021)"]$src_pop_1,
        fmt_fixed_decimal(
            input = comp_dt[study == "BFGI (2021)"]$posttax_hh_total_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "BFGI (2021)"]$posttax_winner_wage_estimate, #nolint
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "BFGI (2021)"]$pretax_hh_wage_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_a_row2_input_a, file_dir = filedir, append = TRUE)

panel_a_row2_input_b <-
    sprintf(
        " & %s &  &  &  & (%s)  ", #nolint
        comp_dt[study == "BFGI (2021)"]$src_pop_2,
        fmt_fixed_decimal(
            input = comp_dt[study == "BFGI (2021)"]$pretax_hh_wage_se,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_a_row2_input_b, file_dir = filedir, append = TRUE)

write_latex_table_row(
        "  &  &  &  &  & ",
        file_dir = filedir,
        short_row_height = TRUE,
        append = TRUE
    )

panel_a_row3_input_a <-
    sprintf(
        "\\multirow{2}{*}{%s} & %s & %s & %s & & %s", #nolint
        comp_dt[study == "Our estimate"]$study,
        comp_dt[study == "Our estimate"]$src_pop_1,
        fmt_fixed_decimal(
            input = comp_dt[study == "Our estimate"]$posttax_hh_total_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "Our estimate"]$posttax_winner_wage_estimate, #nolint
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "Our estimate"]$pretax_hh_wage_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_a_row3_input_a, file_dir = filedir, append = TRUE)

panel_a_row3_input_b <-
    sprintf(
        " & %s & (%s) & (%s) & & (%s)", #nolint
        comp_dt[study == "Our estimate"]$src_pop_2,
        fmt_fixed_decimal(
            input = comp_dt[study == "Our estimate"]$posttax_hh_total_se,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "Our estimate"]$posttax_winner_wage_se, #nolint
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "Our estimate"]$pretax_hh_wage_se,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_a_row3_input_b, file_dir = filedir, append = TRUE)

write_latex_table_row(
        "  &  &  &  &  & ",
        file_dir = filedir,
        short_row_height = TRUE,
        append = TRUE
    )

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

write_latex_table_row(
    "\\emph{Panel B: European Studies} &  &  &  &  &  ",
    file_dir = filedir,
    append = TRUE
)
write_latex_table_row(
        "  &  &  &  &  & ",
        file_dir = filedir,
        short_row_height = TRUE,
        append = TRUE
    )

panel_b_row1_input_a <-
    sprintf(
        "\\multirow{2}{*}{%s} & %s & %s & %s & & \\multirow{2}{*}{%s}  ", #nolint
        comp_dt[study == "CLNÖ (2017)"]$study,
        comp_dt[study == "CLNÖ (2017)"]$src_pop_1,
        fmt_fixed_decimal(
            input = comp_dt[study == "CLNÖ (2017)"]$posttax_hh_total_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "CLNÖ (2017)"]$posttax_winner_wage_estimate, #nolint
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "CLNÖ (2017)"]$pretax_hh_wage_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_b_row1_input_a, file_dir = filedir, append = TRUE)

panel_b_row1_input_b <-
    sprintf(
        " & %s & (%s) & (%s) & &   ", #nolint
        comp_dt[study == "CLNÖ (2017)"]$src_pop_2,
        fmt_fixed_decimal(
            input = comp_dt[study == "CLNÖ (2017)"]$posttax_hh_total_se,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "CLNÖ (2017)"]$posttax_winner_wage_se, #nolint
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_b_row1_input_b, file_dir = filedir, append = TRUE)

write_latex_table_row(
        "  &  &  &  &  & ",
        file_dir = filedir,
        short_row_height = TRUE,
        append = TRUE
    )

panel_b_row2_input_a <-
    sprintf(
        "\\multirow{2}{*}{%s} & %s & \\multirow{2}{*}{%s} & %s & & \\multirow{2}{*}{%s}  ", #nolint
        comp_dt[study == "PSO (2018)"]$study,
        comp_dt[study == "PSO (2018)"]$src_pop_1,
        fmt_fixed_decimal(
            input = comp_dt[study == "PSO (2018)"]$posttax_hh_total_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "PSO (2018)"]$posttax_winner_wage_estimate, #nolint
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        ),
        fmt_fixed_decimal(
            input = comp_dt[study == "PSO (2018)"]$pretax_hh_wage_estimate,
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_b_row2_input_a, file_dir = filedir, append = TRUE)

panel_b_row2_input_b <-
    sprintf(
        " & %s &  & (%s) & &   ", #nolint
        comp_dt[study == "PSO (2018)"]$src_pop_2,
        fmt_fixed_decimal(
            input = comp_dt[study == "PSO (2018)"]$posttax_winner_wage_se, #nolint
            sub_for_missing = "NA",
            should_be_rounded = TRUE,
            round_digits = 3L,
            decimal_digits = 3L,
            scale_factor = NA,
            add_comma = FALSE
        )
    )
write_latex_table_row(panel_b_row2_input_b, file_dir = filedir, append = TRUE)

write_latex_table_row(
        "  &  &  &  &  & ",
        file_dir = filedir,
        short_row_height = TRUE,
        append = TRUE
    )

# End Table 6.1
add_latex_table_rule(
    rule_type = "bottomrule",
    file_dir = filedir,
    append = TRUE
)
end_latex_table(file_dir = filedir, append = TRUE)
filedir <- NULL
rm(filedir)

# Housekeeping
comp_dt <- NULL
panel_a_heading_input1a <- NULL
panel_a_heading_input1b <- NULL
panel_a_heading_input1c <- NULL
panel_a_row1_input <- NULL
panel_a_row2_input_a <- NULL
panel_a_row2_input_b <- NULL
panel_a_row3_input_a <- NULL
panel_a_row3_input_b <- NULL
panel_b_row1_input_a <- NULL
panel_b_row1_input_b <- NULL
panel_b_row2_input_a <- NULL
panel_b_row2_input_b <- NULL
rm(
    comp_dt,
    panel_a_heading_input1a,
    panel_a_heading_input1b,
    panel_a_heading_input1c,
    panel_a_row1_input,
    panel_a_row2_input_a,
    panel_a_row2_input_b,
    panel_a_row3_input_a,
    panel_a_row3_input_b,
    panel_b_row1_input_a,
    panel_b_row1_input_b,
    panel_b_row2_input_a,
    panel_b_row2_input_b
)